﻿using System.Collections.Generic;
using System.Configuration;
using good1.ApplicationServices.Dto;
using good1.ApplicationServices.Interface;
using good1.Core;
using good1.Core.Interfaces.Data;
using SharpArch.Core;
using SharpArch.Core.PersistenceSupport;

namespace good1.ApplicationServices
{
    public class ProjectService : IProjectService<ProjectDto>
    {
        public ProjectService(IRepository<Charity> charityRepository,
            IRepository<Location> locationRepository, IProjectRepository projectRepository)
        {
            Check.Require(projectRepository != null, "projectRepository may not be null");
            Check.Require(charityRepository != null, "charityRepository may not be null");
            Check.Require(locationRepository != null, "locationRepository may not be null");
            this.projectRepository = projectRepository;
            this.charityRepository = charityRepository;
            this.locationRepository = locationRepository;
        }


        public ProjectDto Get(int id)
        {
            ProjectDto dto = new ProjectDto(projectRepository.Get(id));
            dto.Charities = charityRepository.GetAll();
            dto.Locations = locationRepository.GetAll();
            return dto;
        }

        public IList<Project> GetAllSorted()
        {
            return projectRepository.GetAllSorted();
        }

        public IList<Project> GetFeatured()
        {
            int featuredProjectCount = !string.IsNullOrEmpty(ConfigurationManager.AppSettings["FeaturedProjectCount"]) ? 10 : int.Parse(ConfigurationManager.AppSettings["FeaturedProjectCount"]); 
            return projectRepository.GetPage(0, featuredProjectCount); 
        }
        
        public IList<Project> Search(string query)
        {
            return projectRepository.Search(query, 0, 10);
        }

        public void SaveOrUpdate(Project project)
        {
            projectRepository.SaveOrUpdate(project);
        }

        public string Delete(int id)
        {
            string resultMessage = "The project was successfully deleted.";
            Project projectToDelete = projectRepository.Get(id);

            if (projectToDelete != null)
            {
                projectRepository.Delete(projectToDelete);

                try
                {
                    projectRepository.DbContext.CommitChanges();
                }
                catch
                {
                    resultMessage = "A problem was encountered preventing the project from being deleted. " +
                        "Another item likely depends on this project.";
                    projectRepository.DbContext.RollbackTransaction();
                }
            }
            else
            {
                resultMessage = "The project could not be found for deletion. It may already have been deleted.";
            }
            return resultMessage;
        }

        public void RollbackTransaction()
        {
            projectRepository.DbContext.RollbackTransaction();
        }

        public ProjectDto GetCreationDto()
        {
            ProjectDto dto = new ProjectDto(new Project());

            dto.Charities = charityRepository.GetAll();
            dto.Locations = locationRepository.GetAll();
            return dto;
        }

        public Charity GetCharity(int id)
        {
            return charityRepository.Get(id);
        }

        public Location GetLocation(int id)
        {
            return locationRepository.Get(id);
        }

        private readonly IProjectRepository projectRepository;
        private readonly IRepository<Charity> charityRepository;
        private readonly IRepository<Location> locationRepository;
    
    }

  
}
